;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Enterprise.WechatDocuments", [])
        .controller("WechatDocumentsCtrl", ["$scope", "Upload", "ybWidget", "ybHttp", "MESG_TYPE", "UserContextService", "ybConfig", function ($scope, Upload, ybWidget, ybHttp, MESG_TYPE, UserContextService, ybConfig) {
            $scope.activeFlag = [true, false];
            $scope.submitted = false;
            $scope.dataList = [];
            $scope.fileList = [];
            $scope.detailInfo = {};

            var btnPermissionCode = {
                weChatDocumentEdit: window.ActionCode.weChatDocumentEdit
            };

            $scope.btnPermission = {
                weChatDocumentEdit: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false, false, false];
                $scope.activeFlag[index] = true;
                $scope.submitted = false;
                switch (index) {
                    case 0:
                        initData();
                        break;
                    case 1:
                        break;
                    default:
                        break;
                }
            };

            function initData() {
                $scope.detailInfo = {};
                $scope.fileList = [];
            }

            // 删除文件
            $scope.deleteEditDoc = function (index, id) {
                var url = '/wechat/deleteDoc';
                ybHttp.Get('device', url, {id: id}).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return false;
                    }
                    ybWidget.ShowToaster("success", "操作成功");
                    $scope.fileList.splice(index, 1);
                })
            };

            // 下载附件
            $scope.downloadAttach = function (id) {
                window.location.href = ybHttp.RequestUrl("device", "/wechat/downloadAttach") + '?id=' + id;
            };

            // 上传文件
            $scope.uploadDoc = function (file) {
                if (!file) {
                    return;
                }
                if (!/\.(xlsx|xls|doc|docx|pdf|zip|rar|XLSX|XLS|DOC|DOCX|PDF)$/.test(file.name)) {
                    ybWidget.ShowMessage("请选择 .xlsx .xls .doc .docx .pdf .zip .rar  格式的文件，最大不能超过50M", MESG_TYPE.Error);
                    return;
                }
                return Upload.upload({
                    url: ybConfig.getConfig().baseUrl.UploadSite + ybConfig.getConfig().serviceName.device + '/wechat/uploadAttachs',
                    data: {file: file, fileType: $scope.detailInfo.categoryType}
                }).then(function (res) {
                    if (res.data && res.data.code !== 0) {
                        ybWidget.ShowMessage(res.data, MESG_TYPE.Error);
                        return false;
                    }
                    getCategoryFiles($scope.detailInfo.categoryType);
                    return true;
                }, function (res) {
                    ybWidget.ShowMessage(res, MESG_TYPE.Error);
                    return false;
                });
            };

            // 编辑
            $scope.editData = function (item) {
                getCategoryFiles(item.categoryType).then(function (data) {
                    if (data) {
                        $scope.detailInfo = item;
                        $scope.changeTab(1);
                    }
                })
            };

            // 获取分类下的文件列表
            function getCategoryFiles (type) {
                var url = '/wechat/listAttachmentsByType';
                return ybHttp.Get('device', url, {fileType: type}).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return false;
                    }
                    $scope.fileList = res.value;
                    return true;
                });
            }

            // 获取文件分类列表
            function getFileCategories () {
                var url = '/wechat/listFileCategorys';
                ybHttp.Get('device', url).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    $scope.dataList = res.value;
                })
            }

            (function () {
                getFileCategories();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
    ;
})(window, window.angular);